package main

import (
	list "container/list"
	"database/sql"
	"fmt"
	_"github.com/go-sql-driver/mysql"
)

var CurrentNumber = 100001

func SendFatRateCircle(publishUserId string, publishUsername string, publishAge int, publishStature float64, publishWeight float64, publishFatRate float64) (publishId string) {
	fatRateCircle := NewFatRateCircle(publishUserId, publishUsername, publishAge, publishStature, publishWeight, publishFatRate)
	publishId = saveFatRateCircle(fatRateCircle)
	return publishId
}

func DeleteOne(publishId string) (deleteStatus int) {
	deleteFatRateCircle(publishId)
	return 1
}

func StrollFatRateCircle() *list.List {
	queryList := selectFatRateCircle()
	return queryList
}


type FatRateCircle struct {

	id string // 主键

	publishTime string // 发布时间

	publishUserId string // 发布用户

	publishUsername stirng // 发布用户

	publishAge int // 发布用户年龄

	publishStature float64 // 发布用户身高

	publishWeight float64 // 发布用户体重

	publishFatRate float64 // 发布用户体脂率

	publishStatus string // 发布状态

	updateTime string // 更新时间
}

func NewFatRateCircle(publishUserId string, publishUsername string, publishAge int, publishStature float64, publishWeight float64, publishFatRate float64 ) *FatRateCircle {
	fatRateCircle := new(FatRateCircle)
	fatRateCircle.id = currentId()
	fatRateCircle.publishTime = currentTime()
	fatRateCircle.publishUserId = publishUserId
	fatRateCircle.publishUsername = publishUsername
	fatRateCircle.publishAge = publishAge
	fatRateCircle.publishStature = publishStature
	fatRateCircle.publishWeight = publishWeight
	fatRateCircle.publishFatRate = publishFatRate
	fatRateCircle.publishStatus = "normal"
	fatRateCircle.updateTime = publishTime
	return fatRateCircle
}

func QueryNewFatRateCircle(id string, publishTime string, publishUserId string, publishUsername string, publishAge int, publishStature float64, publishWeight float64, publishFatRate float64, publishStatus string, updateTime string) *FatRateCircle {
	fatRateCircle := new(FatRateCircle)
	fatRateCircle.id = id
	fatRateCircle.publishTime = publishTime
	fatRateCircle.publishUserId = publishUserId
	fatRateCircle.publishUsername = publishUsername
	fatRateCircle.publishAge = publishAge
	fatRateCircle.publishStature = publishStature
	fatRateCircle.publishWeight = publishWeight
	fatRateCircle.publishFatRate = publishFatRate
	fatRateCircle.publishStatus = publishStatus
	fatRateCircle.updateTime = updateTime

	return fatRateCircle
}

func saveFatRateCircle(fatRateCircle *FatRateCircle) (publishId string) {
	db, _ := sql.Open("mysql", "godemo:godemo@tcp(127.0.0.1:3306)/godb?charset=utf8")
	insertSql := "insert into go_0_w16_fat_rat (id, publish_time, publish_user_id, publish_username, publish_age, publish_stature, publish_weight, publish_fat_rate, publish_status, update_time ) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"


	result, _ := db.Exec(insertSql, fatRateCircle.id, fatRateCircle.publishTime, fatRateCircle.publishUserId, fatRateCircle.publishUsername, fatRateCircle.publishAge, fatRateCircle.publishStature, fatRateCircle.publishWeight, fatRateCircle.publishFatRate, fatRateCircle.publishStatus, fatRateCircle.updateTime)
	fmt.Println("insert result", result)
}

func deleteFatRateCircle(id string) {
	db, _ := sql.Open("mysql", "godemo:godemo@tcp(127.0.0.1:3306)/godb?charset=utf8")
	deleteSql := "update go_0_w16_fat_rate set publish_status = ?, update_time = ? where id = ?"
	result, _ := db.Exec(deleteSql, "delete", currentTime() , id)
	fmt.Println("delete result ", result)
}

func selectFatRateCircle() *list.List {
	circleList := list.New()
	db, _ := sql.Open("mysql", "godemo:godemo@tcp(127.0.0.1:3306)/godb?charset=utf8")
	sql := "select id, publish_time, publish_user_id, publish_username, publish_age, publish_stature, publish_weight, publish_fat_rate, publish_status, update_time from go_0_w16_fat_rate where publish_status = 'normal' order by publish_time desc , id desc limit 30"
	row, _ := db.Query(sql)
	//var count int
	//row.Scan(&count)
	for row.Next() {
		var id string
		var publishTime string
		var publishUserId string
		var publishUsername string
		var publishAge int
		var publishStature float64
		var publishWeight float64
		var publishFatRate float64
		var publishStatus string
		var updateTime string
		row.Scan(&id, &publishTime, &publishUserId, &publishUsername, &publishAge, &publishStature, &publishWeight, &publishFatRate, &publishStatus, &updateTime)
		fatRateCircle := QueryNewFatRateCircle(id, publishTime, publishUserId, publishUsername, publishAge, publishStature, publishWeight, publishFatRate, publishStatus, updateTime)

		circleList.PushBack(fatRateCircle)

	}


	return circleList
}

func currentTime()  string{
	return "2022-03-27 11:22:33"
}

func currentId() string {
	id := string(CurrentNumber)
	CurrentNumber++
	return id
}


func main() {

	fmt.Println("第十六周 第六次作业")

	publishUserId := "60012"
	publishUsername := "测试用户"
	publishAge := 22
	publishStature := 1.671
	publishWeight := 65.12
	publishFatRate := 33.12
	// 发送
	publishId := SendFatRateCircle(publishUserId, publishUsername, publishAge, publishStature, publishWeight, publishFatRate)

	// 删除
	deleteFatRateCircle(publishId)

	// 查询




}
